//
//  leftViewController.m
//  ceshi
//
//  Created by MWJ on 15/4/23.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "leftViewController.h"
#import "LeftView.h"
#import "AppDelegate.h"
#import "DataCell.h"    //个人资料的cell
#import "SetUpViewController.h"
#import "PostDetailViewController.h"
#import "personPost.h"
#define personalDataURL @"http://192.168.1.110:8888/hesaid/index.php/Hesaid/PersonalCenter" //个人资料URL
#define personalpostURL @"http://192.168.1.110:8888/hesaid/index.php/Hesaid/PersonalCenterPost"

@interface leftViewController (){
    LeftView *leftView;
    NSMutableArray *personArray;
    NSMutableArray *personPostArray;
    NSMutableArray *postArray;
    AppDelegate *app;
}

@end

@implementation leftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    leftView.LeftTableView.dataSource = nil;
    personArray = [NSMutableArray array];
    postArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor clearColor];
    self.navigationItem.title=@"个人中心";
    
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    
    //创建“设置按钮”
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed: @"setUp.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClick)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    //加载自定义的scrollview
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"LeftView" owner:self options:nil];
    leftView = nib[0];
    //把自定义的scrollview添加到框架scrollview上
    [self.scrollerView addSubview:leftView];
    [self loading];
    [self loadingPost];
}

-(void)loading{
    NSURL *url=[NSURL URLWithString:personalDataURL];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *poststr =[NSString stringWithFormat:@"user_id=%@",app.user_id];
    NSData *data=[poststr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        personArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:personArray waitUntilDone:YES];
        for (NSDictionary *dic in personArray) {
            leftView.phoneLabel.text= [NSString stringWithFormat:@"%@",dic[@"user_phone"]];
            NSString *headportrait = [NSString stringWithFormat:@"%@",dic[@"user_HeadPortrait"]];
            NSURL *url=[NSURL URLWithString:headportrait];
            leftView.headPortrait.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
        }
    }];
    
}
-(void)loadingPost{
    personPostArray = [NSMutableArray array];
    NSURL *url=[NSURL URLWithString:personalpostURL];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSString *poststr =[NSString stringWithFormat:@"user_id=%@",app.user_id];
    NSData *data=[poststr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        personPostArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData) withObject:personPostArray waitUntilDone:YES];
        for (NSDictionary *dic in personPostArray) {
            personPost *p = [[personPost alloc]init];
            p.postcontent = dic[@"message_content"];
            p.postreply = dic[@"message_reply"];
            p.posttime = dic[@"message_time"];
            p.postshowtime = dic[@"message_showtime"];
            [postArray addObject:p];
        }
    }];
}
-(void)refreshData{
    
}
#pragma mark - 导航栏右边按钮点击事件
-(void)rightItemClick{
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SetUpViewController *setUp = [storyboard instantiateViewControllerWithIdentifier:@"setUpVC"];
    [self.navigationController pushViewController:setUp animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma  mark - cell的个数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return postArray.count;
}

#pragma  mark - 每一行cell的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

#pragma  mark - cell的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UINib *nib=[UINib nibWithNibName:@"DataCell" bundle:nil];
    [leftView.LeftTableView registerNib:nib forCellReuseIdentifier:@"data"];
    
    static NSString *cellIdentifier=@"data";
    DataCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    personPost *pp = postArray[indexPath.row];
    cell.time.text = [NSString stringWithFormat:@"%@",pp.posttime];
    cell.reply.text=[NSString stringWithFormat:@"%@",pp.postreply];
    cell.content.text=[NSString stringWithFormat:@"%@",pp.postcontent];
    cell.ShowTime.text=[NSString stringWithFormat:@"%@",pp.postshowtime];
//    cell.time.text=@"2014-09-10 12:03:35";
//    cell.content.text=@"真正有气质的淑女，从不炫耀她所拥有的一切，她不告诉人她读过什么书，去过什么地方，有多少件衣服，买过什么珠宝，因为她没有自卑感。——《圆舞》";
//    cell.ShowTime.text=@"7";
    
    return cell;
}
#pragma  mark - cell的点击事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    PostDetailViewController *postDetail = [storyboard instantiateViewControllerWithIdentifier:@"postDetailVC"];
    [self.navigationController pushViewController:postDetail animated:YES];
    
}


@end
